# Built-In Example AI

# Title: CircleTurtle
# Author: Adam Rumpf
# Version: 1.0.0
# Date: 11/12/2020
# zuiudisj
import game.tcturtle
import random
import datetime
import math

class CombatTurtle(game.tcturtle.TurtleParent):
    """Test circular combat turtle.

    This turtle simply travels in circles and periodically shoots.
    """

    #-------------------------------------------------------------------------
    
    is_turn = 0
    no_shoot_start = datetime.datetime.now()
    no_shoot_end = datetime.datetime.now()
    
    #-------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.
        """

        return "kx_nisuodedoudui"

    #-------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.
        """

        return "I don't agree anything with you say."

    #-------------------------------------------------------------------------

    def class_shape():
        """CombatTurtle.class_shape() -> (int or tuple)
        Static method to define the Combat Turtle's shape image.

        The return value can be either an integer or a tuple of tuples.

        Returning an integer index selects one of the following preset shapes:
            0 -- arrowhead (also default in case of unrecognized index)
            1 -- turtle
            2 -- plow
            3 -- triangle
            4 -- kite
            5 -- pentagon
            6 -- hexagon
            7 -- star

        A custom shape can be defined by returning a tuple of the form
        (radius, angle), where radius is a tuple of radii and angle is a tuple
        of angles (in radians) describing the polar coordinates of a polygon's
        vertices. The shape coordinates should be given for a turtle facing
        east.
        """

        return 6

    #=========================================================================

    def setup(self):
        """CombatTurtle.setup() -> None
        Initialization code for Combat Turtle.
        """

        # Define constant linear and angular speed factors
        self.spd = 1.0
        self.turn_spd = 0.5

    #-------------------------------------------------------------------------
    def heading_by_angle(self,angle=0):
        target = self.other_position

        if type(target) == int or type(target) == float:
            target = (int(self.x + 10000*math.cos(math.radians(target))),int(self.y - 10000*math.sin(math.radians(target))))

        if type(target) == tuple:
            turn = self.relative_heading_towards(target)
            turn = turn + angle
            if turn > 180:
                turn = turn - 360
            if turn < -180:
                turn = turn + 360
            turn = min(max(turn, -self.max_turn_speed),self.max_turn_speed)
            turn /= self.max_turn_speed
            self.left(turn)

    def step(self):
        """CombatTurtle.setup() -> None
        Step event code for Combat Turtle.
        """
        
        dir = self.distance()
        dir_keep = 150
        
        #datetime.datetime.now()
        if not self._other.can_shoot :
            self.no_shoot_start = datetime.datetime.now()
            self.no_shoot_end = datetime.datetime.now()
        else :
            self.no_shoot_end = datetime.datetime.now()
        
        between_shoot_time = (self.no_shoot_end-self.no_shoot_start).seconds
        
        # 危险状态躲避子弹
        if self._other.cooldown>35 or (self._other.cooldown<25 and dir<dir_keep and between_shoot_time < 10) :
            # print("self.is_turn = ",self.is_turn)
            if self.is_turn == 0:
                self.is_turn = random.randint(1,4)
            if self.is_turn==1:
                self.heading_by_angle(90)
                self.forward()
            elif self.is_turn==2:
                self.heading_by_angle(-90)
                self.forward()
            elif self.is_turn==3:
                self.heading_by_angle(90)
                self.backward()
            else :
                self.heading_by_angle(-90)
                self.backward()
        # 安全时朝向敌方
        else :
            if dir > 150:
                self.heading_by_angle(random.randint(-90, 90))
                self.forward()
            else:
                self.is_turn = 0
                self.heading_by_angle(0)
                if dir > dir_keep:
                    self.forward()
                else:
                    self.backward()

        if self._other.cooldown==30 or (self.relative_heading_towards()==0 and between_shoot_time >= 10):
            #if self.can_shoot :
            if (self.can_shoot and self.line_of_sight()):#朝无障碍物方向射击
                self.shoot()
